﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MultiAxisAssembly;
using ZRFCore.MachineVisionAlgorithm.Controls.ZrfWindowControl;

namespace 喷点胶机
{
    public delegate void StringEventHandler(string str);
    public partial class Page_0_HomePage : UIPage
    {
        private ControlMaster controlMaster;
        public event StringEventHandler? OpenProjEvent;
        public event StringEventHandler? SaveProjEvent;
        public Page_0_HomePage(ControlMaster controlMaster)
        {
            InitializeComponent();
            this.controlMaster = controlMaster;
            //this.controlMaster.StartProjWorkEvent += ControlMaster_StartProjWorkEvent1;
            this.controlMaster.FindedProjAllXldPathsEvent += ControlMaster_FindedProjAllXldPathsEvent;
            zrf_hWindow1.EnEdit = false;
            zrf_hWindow1.EnMenuStrip = false;
        }

        private void ControlMaster_FindedProjAllXldPathsEvent(HalconDotNet.HImage newImg, List<ZRFCore.MachineVisionAlgorithm.Controls.ZrfWindowControl.Path.XldPath> xldPaths)
        {
            this.Invoke(new Action(() =>
            {
                zrf_hWindow1.ClearWindow();
                if (xldPaths.Count == 0)
                {
                    zrf_hWindow1.DispTextOnWindow("未找到产品", 30, 30, "red");
                }
                zrf_hWindow1.XldPaths = xldPaths;
                zrf_hWindow1.DispImage(newImg);
            }));
        }

        private void ControlMaster_StartProjWorkEvent1(HalconDotNet.HImage newImg, List<ZRFCore.MachineVisionAlgorithm.Controls.ZrfWindowControl.Path.XldPath> allXldPaths)
        {

        }

        private void ControlMaster_StartProjWorkEvent(HalconDotNet.HImage newImg, List<HalconDotNet.HXLD[]> allXldPaths)
        {
            this.Invoke(new Action(() =>
            {
                zrf_hWindow1.HXLDs.Clear();
                for (int i = 0; i < allXldPaths.Count; i++)
                {
                    for (int j = 0; j < allXldPaths[i].Length; j++)
                    {
                        zrf_hWindow1.HXLDs.Add(new ZRFCore.MachineVisionAlgorithm.Controls.ZrfWindowControl.Object.HXLDWithColor(allXldPaths[i][j], "yellow"));
                    }
                }
                zrf_hWindow1.DispImage(newImg);
            }));
        }

        private void btEmergentStop_Click(object sender, EventArgs e)
        {
            try
            {
                controlMaster.MachineEmergentStop();

            }
            catch (Exception ee)
            {

                ShowWarningDialog(ee.Message);
            }
        }

        private void btResetEmergentStop_Click(object sender, EventArgs e)
        {
            try
            {
                controlMaster.MachineResetEmergentStop();

            }
            catch (Exception ee)
            {

                ShowWarningDialog(ee.Message);
            }
        }

        private void btOpenProj_Click(object sender, EventArgs e)
        {
            try
            {
                string projName = controlMaster.ReadProj();
                if (OpenProjEvent != null)
                {
                    OpenProjEvent.Invoke(projName);
                }
            }
            catch (Exception ee)
            {

                ShowWarningDialog(ee.Message);
            }

        }

        private void btSaveProj_Click(object sender, EventArgs e)
        {
            try
            {
                string projName = controlMaster.SaveProj();
                if (SaveProjEvent != null)
                {
                    SaveProjEvent.Invoke(projName);
                }
            }
            catch (Exception ee)
            {
                ShowWarningDialog(ee.Message);
            }

        }

        private void Page_0_HomePage_Initialize(object sender, EventArgs e)
        {
            controlMaster.ReadyToWork();
            timer1.Enabled = true;
        }

        private void Page_0_HomePage_Finalize(object sender, EventArgs e)
        {
            controlMaster.EndReadyToWork();
            timer1.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            uiLabel1.Text = "生产数量:" + controlMaster.ProductionQuantity;
            if (controlMaster.MachineProEx.MotionStatus.IsEmergentStop)
            {
                btEmergentStop.Enabled = false;
                btResetEmergentStop.Enabled = true;
            }
            else
            {
                btEmergentStop.Enabled = true;
                btResetEmergentStop.Enabled = false;
            }
        }
    }
}
